package com.santu.edu.supporter.util;

import cn.hutool.core.collection.CollUtil;
import lombok.NonNull;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author luoyaoqi
 * @createTime 2024/2/21
 * @description
 */
public class PageData<T> implements Serializable {
    Long pages;
    Long total;
    List<T> list;

    public PageData() {
    }

    public PageData(List<T> list, long pages, long total) {
        this.list = list;
        this.pages = pages;
        this.total = total;
    }

    /**
     * EsPage 对象转换
     *
     * @param mapper 映射函数
     * @param <R>    结果类型
     * @return 对象
     */
    public <R> PageData<R> convert(@NonNull Function<T, R> mapper) {
        final PageData<R> page = new PageData<>();
        page.setPages(this.pages);
        page.setTotal(this.total);

        if (CollUtil.isNotEmpty(this.list)) {
            final List<R> data = this.list.stream().map(mapper).collect(Collectors.toList());
            page.setList(data);
        } else {
            page.setList(Collections.emptyList());
        }
        return page;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Long getPages() {
        return pages;
    }

    public void setPages(Long pages) {
        this.pages = pages;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
